﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenLeuvenDeepZoom
{
    public partial class MainPage : UserControl
    {
        Point lastMousePos = new Point();
        Point lastMouseLogicalPos = new Point();
        Point lastMouseViewPort = new Point();
        bool duringDrag = false;
        double zoom = 1;
        double minzoom = 0.001;
        bool duringOpen = false;

        public MainPage()
        {
            InitializeComponent();
            this.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                this.lastMousePos = e.GetPosition(this.DeepZoom);
            };

            DeepZoom.MouseWheel += new MouseWheelEventHandler(DeepZoom_MouseWheel);

        }
        public void DeepZoom_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            double newzoom = zoom;
            if (e.Delta > 0)
                newzoom /= 1.3;
            else
                newzoom *= 1.3;
            Point logicalPoint = this.DeepZoom.ElementToLogicalPoint(this.lastMousePos);

            this.DeepZoom.ZoomAboutLogicalPoint(zoom / newzoom, logicalPoint.X, logicalPoint.Y);
            zoom = newzoom;
            e.Handled = true;
        }

        private void Home_Click(object sender, MouseButtonEventArgs e)
        {
            this.DeepZoom.ViewportOrigin = new Point(0, 0);
            this.DeepZoom.ViewportWidth = 1;
            zoom = 1;
        }

        private void ZoomIn_Click(object sender, MouseButtonEventArgs e)
        {
            double newzoom = zoom / 1.3;
            if (newzoom < minzoom) { newzoom = minzoom; }
            Point logicalPoint = this.DeepZoom.ElementToLogicalPoint(new Point(this.ActualWidth / 2, this.ActualHeight / 2));
            this.DeepZoom.ZoomAboutLogicalPoint(zoom / newzoom, logicalPoint.X, logicalPoint.Y);
            zoom = newzoom;
        }

        private void ZoomOut_Click(object sender, MouseButtonEventArgs e)
        {
            double newzoom = zoom * 1.3;
            if (newzoom > 1) { newzoom = 1; }
            Point logicalPoint = this.DeepZoom.ElementToLogicalPoint(new Point(this.ActualWidth / 2, this.ActualHeight / 2));
            this.DeepZoom.ZoomAboutLogicalPoint(zoom / newzoom, logicalPoint.X, logicalPoint.Y);
            zoom = newzoom;
        }

        private void DeepZoom_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            lastMouseLogicalPos = e.GetPosition(this.DeepZoom);
            lastMouseViewPort = this.DeepZoom.ViewportOrigin;
            duringDrag = true;
        }

        private void DeepZoom_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            duringDrag = false;
            this.DeepZoom.UseSprings = true;
        }

        private void DeepZoom_MouseMove(object sender, MouseEventArgs e)
        {
            if (duringDrag)
            {
                Point newPoint = lastMouseViewPort;
                Point thisMouseLogicalPos = e.GetPosition(this.DeepZoom);
                newPoint.X += (lastMouseLogicalPos.X - thisMouseLogicalPos.X) / this.DeepZoom.ActualWidth * this.DeepZoom.ViewportWidth;
                newPoint.Y += (lastMouseLogicalPos.Y - thisMouseLogicalPos.Y) / this.DeepZoom.ActualWidth * this.DeepZoom.ViewportWidth;
                this.DeepZoom.ViewportOrigin = newPoint;
            }
        }

        private void DeepZoom_ImageOpenSucceeded(object sender, RoutedEventArgs e)
        {
            duringOpen = true;

        }

        private void DeepZoom_MotionFinished(object sender, RoutedEventArgs e)
        {
            if (duringOpen)
            {
                duringOpen = false;

                // beetje uitzoomen
                this.DeepZoom.ViewportOrigin = new Point(-0.4, -0.1);
                this.DeepZoom.ViewportWidth = 1.8;
            }
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Z)
            {

                this.DeepZoom.ZoomAboutLogicalPoint(1.2, 0.5, 0.5);
                e.Handled = true;

            }
            else if (e.Key == Key.X)
            {
                this.DeepZoom.ZoomAboutLogicalPoint(0.8, 0.5, 0.5);
                e.Handled = true;
            }
        }
    }
}

